package cn.wangliang181230.boot.web.autoconfiguration;

import cn.wangliang181230.boot.web.beanpostprocessor.MyBeanPostProcessor;
import cn.wangliang181230.boot.web.properties.MyTestProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
//@EnableConfigurationProperties(MyTestProperties.class)
public class MyAutoConfiguration {

	public MyAutoConfiguration() {
		System.out.println("MyTestAutoConfiguration: constructor");
	}


	// 注意 BeanPostProcessor 是一个特殊的Bean，它需要处理其他Bean，所以它要优先于其他Bean被创建
	// 所以此方法上需要添加 static，使其能够被优先创建，而不依赖于当前类的实例化。
	@Bean
	public static MyBeanPostProcessor defaultMyBeanPostProcessor() {
		System.out.println("MyTestAutoConfiguration: create bean: MyBeanPostProcessor");
		return new MyBeanPostProcessor();
	}


	@Bean
	@ConfigurationProperties("my.test")
	public MyTestProperties defaultMyTestProperties(@Value("${spring.profiles.active:prod}") String env) {
		System.out.println("MyTestAutoConfiguration: create bean: defaultMyTestProperties");
		MyTestProperties properties = new MyTestProperties();

		properties.setString("application-" + env);

		return properties;
	}

}
